import serial

BAUDRATE = 9600

def measure() :
    port.write('M')
    print "Waiting...",
    m = port.readline()
    print m

def calibrate() :
    port.write('Q')
    print "Calibrating...",
    m = port.readline()
    print m
	
if __name__ == "__main__":
    port = serial.Serial("COM3", BAUDRATE)
    print "Connection opened"

    # Handshake
    port.write('A')
    print "Handshake completed"
    
    while(1):
        x = raw_input()
        if (len(x) == 1 and x == 'M') :
            measure()
        elif (len(x) == 1 and x == 'Q') :
            calibrate()
        elif (len(x) == 1 and (x == 'L' or x == 'D')) :
            port.write(x);
        elif (len(x) == 1) :
            break

    port.close()
